package com.smart.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

import com.smart.util.DBUtil;

public class IdentifierCodeDAO {
   public int insert(String identifier,String code) throws SQLException
   {
	    String sql="insert into identifier_code(identifier,code) values(?,?)";
	    Connection conn = DBUtil.getConnection();
		PreparedStatement pSt = conn.prepareCall(sql);
		pSt.setString(1, identifier);
		pSt.setString(2, code);
		return pSt.executeUpdate();
   }
   public String getCode(String identifier) throws SQLException
   {
	   String code=null;
	    String sql="select code from identifier_code where identifier=?";
	    Connection conn = DBUtil.getConnection();
		PreparedStatement pSt = conn.prepareCall(sql);
		pSt.setString(1, identifier);
		ResultSet res=pSt.executeQuery();
		if(res.next())
		{
			code=res.getString("code");
		}
		return code;
   }
   
      public void RmOutDate(Date date) throws SQLException
	  {
		  String sql="delete from identifier_code where create_time <?";
		  Connection conn = DBUtil.getConnection();
		  PreparedStatement pSt = conn.prepareCall(sql);
		  System.out.println(date.toString());
//		  pSt.setString(1,date.toString());
		  pSt.setTimestamp(1,new Timestamp(date.getTime()));
		  pSt.executeUpdate();
	  }
      
      public static void main(String[] args)
      {
    	  IdentifierCodeDAO identifierCodeDAO=new IdentifierCodeDAO();
    	  try {
    		long x=new java.util.Date().getTime();
    		long y=x-2*60000;
    		System.out.println(x);
    		System.out.println(y);
			identifierCodeDAO.RmOutDate(new Date(y));
		} catch (SQLException e) {
			e.printStackTrace();
		}
      }
}
